package com.qf.petmall.starter.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Configuration
public class SwaggerConfig {

    @Bean
    public Docket buildDocket( ) {
        return new Docket( DocumentationType.SWAGGER_2 )
                .apiInfo( buildApiInfo( ) )
                .select( )
                // 要扫描的API(Controller)基础包
                .apis( RequestHandlerSelectors.basePackage( "com.qf.petmall.starter" ) )
                .paths( PathSelectors.any( ) )
                .build( );
    }

    private ApiInfo buildApiInfo( ) {
        Contact contact=new Contact( "同城宠物","http://www.mobiletrain.org/","960397138@qq.com" );
        return new ApiInfoBuilder( )
                .title( "同城宠物API文档" )
                .description( "同城宠物-api" )
                .contact( contact )
                .version( "1.0.0" ).build( );
    }

    @Bean
    public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor( ) {
        return new BeanPostProcessor( ) {
            @Override
            public Object postProcessAfterInitialization( Object bean,String beanName ) throws BeansException {
                if(bean instanceof WebMvcRequestHandlerProvider) {
                    customizeSpringfoxHandlerMappings( getHandlerMappings( bean ) );
                }
                return bean;
            }

            private < T extends RequestMappingInfoHandlerMapping > void customizeSpringfoxHandlerMappings( List< T > mappings ) {
                List< T > copy=mappings.stream( )
                        .filter( mapping->mapping.getPatternParser( )==null )
                        .collect( Collectors.toList( ) );
                mappings.clear( );
                mappings.addAll( copy );
            }

            @SuppressWarnings("unchecked")
            private List< RequestMappingInfoHandlerMapping > getHandlerMappings( Object bean ) {
                try {
                    Field field=ReflectionUtils.findField( bean.getClass( ),"handlerMappings" );
                    field.setAccessible( true );
                    return (List< RequestMappingInfoHandlerMapping >) field.get( bean );
                } catch(IllegalArgumentException|IllegalAccessException e) {
                    throw new IllegalStateException( e );
                }
            }
        };
    }
}

